
$( document ).ready(function() {
   		
	$.get("/db_select", {average: "x"}, function(data){
		set_avg(data);
	});
	
    $.get("/db_select", {middle: "x"}, function(data){
    	set_med(data);
    });
	
    $.get("/db_select", {topten: "x"}, function(data){
    	
    	//data='{"topten": [{"answer": 64, "distance": 36, "name": "Tahat gadol"}, {"answer": 63, "distance": 37, "name": "hgh"}, {"answer": 54, "distance": 46, "name": "Balabalabito long name"}, {"answer": 49, "distance": 51, "name": "Shazam,,"}, {"answer": 35, "distance": 65, "name": "sd"}, {"answer": 30, "distance": 70, "name": "Roi ravhon"}, {"answer": 24, "distance": 76, "name": "Sheker beker"}]}';
    	
    	topten(data);
    });
});

function topten(res) {
	
	var counter=1;
	
	//ret = JSON.parse(res);
	ret = res;
	
	ret.topten.forEach(function (entry){
		
		fill_table(entry, counter);
		counter++;
	});
}

function fill_table(ent, count) {
	
	var name=ent.name;
	var result=ent.answer;
	var distance=ent.distance;
	
	$("#tenbody").append("<tr><td>" + count + "</td><td>" + name + "</td><td>" + result + "</td><td>" + distance + "</td></tr>");
}

function set_avg(res) {
	
	$("#avg_res").html(res);
}

function set_med(res) {
	
	$("#med_res").html(res);
}

function examples()
{
    $.get("/db_select", {topten: "x"});  // top ten - returns json
}